#include <iostream>
using namespace std;

int main()
{
    string s, sub;
    cin >> s;
    int n;
    cin >> n;
    while (n--)
    {
        int b, e, pos;
        string s1, s2;
        cin >> b >> e >> s1 >> s2;
        string p = s1 + s2;
        sub = s.substr(b - 1, e - b + 1);
        s.erase(b - 1, e - b + 1);
        if ((pos = s.find(p)) != s.npos)
        {
            s.insert(pos + s1.length(), sub);
        }
        else
        {
            s.insert(s.length(), sub);
        }
    }
    cout << s;
    return 0;
}